package com.example.administrator.myjdlc.ui;

/**
 * Created by Administrator on 2017/3/21.
 */

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.example.administrator.myjdlc.AppApplication;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;


public class UiManager {
    private static final int UI_BASE_W = 720;
    private static final int UI_BASE_H = 1280;
    public static final int ROUND_CORNERSIZE_ROUND = -1;
    public static int screenWidth;
    public static int screenHeight;
    public static int screenDpi;
    private static float uiScaleX;
    private static float uiScaleY;
    public static float uiScale;
    private static Options uiBitmapOptions;
    private static Resources res;

    public UiManager() {
    }

    public static void init() {
        Context context = AppApplication.getContext();
        res = context.getResources();
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        screenWidth = metrics.widthPixels;
        screenHeight = metrics.heightPixels;
        screenDpi = metrics.densityDpi;
        uiScale = uiScaleX = (float)screenWidth / 720.0F;
        uiScaleY = (float)screenHeight / 1280.0F;
        if(uiScaleY < uiScale) {
            uiScale = uiScaleY;
        }

    }

    public static int scaledDimen(int dimen) {
        return Math.round(uiScale * (float)dimen);
    }

    public static Bitmap decodeBitmap(int resId) {
        InputStream is = null;

        try {
            is = res.openRawResource(resId);
        } catch (NotFoundException var15) {
            ;
        }

        if(is != null) {
            Bitmap bitmap = null;

            try {
                bitmap = BitmapFactory.decodeStream(is, (Rect)null, uiBitmapOptions);
            } catch (OutOfMemoryError var13) {
                var13.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException var12) {
                    var12.printStackTrace();
                }

            }

            return bitmap;
        } else {
            return null;
        }
    }

    public static Bitmap decodeBitmap(String fileName) {
        Bitmap bitmap = null;
        File file = new File(fileName);
        if(file.exists()) {
            FileInputStream fis = null;

            try {
                fis = new FileInputStream(file);
                bitmap = BitmapFactory.decodeStream(fis, (Rect)null, uiBitmapOptions);
            } catch (FileNotFoundException var13) {
                ;
            } finally {
                try {
                    fis.close();
                } catch (IOException var12) {
                    var12.printStackTrace();
                }

            }
        }

        return bitmap;
    }

    public static Bitmap decodeBitmap(InputStream is) {
        return BitmapFactory.decodeStream(is, (Rect)null, uiBitmapOptions);
    }

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
        if(bitmap == null) {
            return null;
        } else {
            Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            if(rect.right > 0 && rect.bottom > 0) {
                if(roundPx < 0.0F) {
                    int output = bitmap.getWidth();
                    int canvas = bitmap.getHeight();
                    roundPx = (float)(output > canvas?canvas / 2:output / 2);
                } else if(roundPx == 0.0F) {
                    return bitmap;
                }

                Bitmap output1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
                Canvas canvas1 = new Canvas(output1);
                int color = -12434878;
                Paint paint = new Paint();
                RectF rectF = new RectF(rect);
                paint.setAntiAlias(true);
                canvas1.drawARGB(0, 0, 0, 0);
                paint.setColor(0xFF424242);
                canvas1.drawRoundRect(rectF, roundPx, roundPx, paint);
                paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
                canvas1.drawBitmap(bitmap, rect, rect, paint);
                return output1;
            } else {
                return null;
            }
        }
    }

    public static Bitmap compressBitmap(Bitmap bitmap, float size) {
        if(bitmap == null) {
            return null;
        } else {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, baos);
            if((float)baos.toByteArray().length <= size) {
                return null;
            } else {
                int quality = 100;

                while((float)baos.toByteArray().length > size) {
                    quality -= 4;
                    baos.reset();
                    if(quality <= 0) {
                        break;
                    }

                    bitmap.compress(CompressFormat.JPEG, quality, baos);
                }

                return BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length);
            }
        }
    }

    public static void hideImm(Activity activity) {
        InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm.isActive()) {
            View currentFocus = activity.getCurrentFocus();
            if(currentFocus != null) {
                imm.hideSoftInputFromWindow(currentFocus.getApplicationWindowToken(), 2);
            }
        }

    }

   /* public static View getContentView() {
        return AppApplication.getTopActivity().findViewById(16908290);
    }*/
}
